//
//  DomesticViewController.m
//  CooperLinker
//
//  Created by 洪雯 on 16/4/13.
//  Copyright © 2016年 洪雯. All rights reserved.
//

#import "DomesticViewController.h"
#import "GroupCollectionCell.h"
#import "GroupSection.h"
#import "UIImageView+WebCache.h"
#import "SYPhotoBrowser.h"
#import "NewSYPhotoBViewController.h"

#define SCREEN_WIDTH ([UIScreen  mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface DomesticViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,SYPhotoBrowserDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
//内容数组
@property (nonatomic, strong) NSMutableArray *dataArray;
//标题数组
@property (nonatomic, strong) NSMutableArray *sectionArray;

@property (nonatomic, strong) NSMutableArray *stateArray;
@end

@implementation DomesticViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUICollectionView];
    [self initData];
}
- (void)createUICollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    //指定布局方式为垂直
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.minimumLineSpacing = 10;//最小行间距(当垂直布局时是行间距，当水平布局时可以理解为列间距)
    flow.minimumInteritemSpacing = 10;//两个单元格之间的最小间距
    
    //创建CollectionView并指定布局对象
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flow];
    //    _collectionView.backgroundColor = [UIColor colorWithRed:239/255.0f green:239/255.0f blue:239/255.0f alpha:1.0];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    
    //注册用xib定制的cell，各参数的含义同UITableViewCell的注册
    [_collectionView registerNib:[UINib nibWithNibName:@"GroupCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"GroupCellID"];
    
    //注册用xib定制的分组脚
    //参数二：用来区分是分组头还是分组脚
    [_collectionView registerNib:[UINib nibWithNibName:@"GroupSection" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GroupSectionID"];
    
}

- (void)initData
{
    NSArray * one = @[@"http://iphone.tgbus.com/UploadFiles/201410/20141020134730646.jpg",
             @"http://a.hiphotos.baidu.com/zhidao/pic/item/2cf5e0fe9925bc314cc6bd685fdf8db1ca1370a2.jpg",
             @"http://d.3987.com/cgqfj_130528/001.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg",
             @"http://news.mydrivers.com/img/20130518/68d97fe443034db3bc3aef4d98ac9188.jpg",
             @"http://d.3987.com/cgqfj_130528/001.jpg"];
    NSArray *two = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    NSArray *three = @[@"http://d.3987.com/cgqfj_130528/005.jpg",@"http://d.3987.com/cgqfj_130528/006.jpg",@"http://d.3987.com/cgqfj_130528/007.jpg"];
    NSArray *four = @[@"http://iphone.tgbus.com/UploadFiles/201410/20141020134730646.jpg",
                      @"http://a.hiphotos.baidu.com/zhidao/pic/item/2cf5e0fe9925bc314cc6bd685fdf8db1ca1370a2.jpg",
                      @"http://d.3987.com/cgqfj_130528/001.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg",
                      @"http://news.mydrivers.com/img/20130518/68d97fe443034db3bc3aef4d98ac9188.jpg",
                      @"http://d.3987.com/cgqfj_130528/001.jpg"];
    NSArray *five = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    NSArray *six = @[@"http://d.3987.com/cgqfj_130528/005.jpg",@"http://d.3987.com/cgqfj_130528/006.jpg",@"http://d.3987.com/cgqfj_130528/007.jpg"];
    NSArray *seven = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    NSArray *eight = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg",@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    NSArray *nine = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    NSArray *ten = @[@"http://d.3987.com/cgqfj_130528/003.jpg",@"http://d.3987.com/cgqfj_130528/004.jpg",@"http://d.3987.com/cgqfj_130528/002.jpg"];
    
    _sectionArray = [NSMutableArray arrayWithObjects:@"河北园",@"岭南园",@"西部园",@"现代园",@"江南园",@"盆景专项园",@"海西州园",@"武汉园",@"皇家园",@"台湾文化专项园", nil];
    
    _dataArray = [NSMutableArray arrayWithObjects:one,two,three,four,five,six,seven,eight,nine,ten,nil];
    
    _stateArray = [NSMutableArray array];
    
    for (int i = 0; i < _dataArray.count; i++)
    {
        //所有的分区都是闭合
        [_stateArray addObject:@"1"];
    }
    
}

//协议的方法,用于返回section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _dataArray.count;
}

//协议中的方法，用于返回分区中的单元格个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([_stateArray[section] isEqualToString:@"1"]) {
        //如果是打开状态
        NSArray *arr = [_dataArray objectAtIndex:section];
        return arr.count;
    }
    else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //UICollectionViewCell里的属性非常少，实际做项目的时候几乎必须以其为基类定制自己的Cell
    GroupCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GroupCellID" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor greenColor];
    
    [cell.iconImage.layer setMasksToBounds:YES];
    [cell.iconImage.layer setCornerRadius:5.0];//设置矩形四个圆角半径
    [cell.iconBtn addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:self.dataArray[indexPath.section][indexPath.row]]];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    NewSYPhotoBViewController *photoBrowser = [[NewSYPhotoBViewController alloc] initWithImageSourceArray:self.dataArray[indexPath.section] delegate:self];
    photoBrowser.userInfo = @"检测卡机电脑卡气温已达不开机";
    photoBrowser.juLi = @"1000m";
    
    [self.navigationController pushViewController:photoBrowser animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}

#pragma mark - SYPhotoBrowser Delegate

- (void)photoBrowser:(SYPhotoBrowser *)photoBrowser didLongPressImage:(UIImage *)image {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"LongPress" message:@"Do somethings" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}


//协议中的方法，用于返回单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH / 3 - 10, SCREEN_WIDTH / 3);
}

//协议中的方法，用于返回整个CollectionView上、左、下、右距四边的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //上、左、下、右的边距
    return UIEdgeInsetsMake(0, 5, 0, 5);
}

//协议中的方法，用来返回分组头的大小。如果不实现这个方法，- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath将不会被调用
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    //宽度随便定，系统会自动取collectionView的宽度
    //高度为分组头的高度
    return CGSizeMake(0, 44);
}


//协议中的方法，用来返回CollectionView的分组头或者分组脚
//参数二：用来区分是分组头还是分组脚
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //重用分组头，因为前边注册过，所以重用一定会成功
    //参数一：用来区分是分组头还是分组脚
    //参数二：注册分组头时指定的ID
    GroupSection *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GroupSectionID" forIndexPath:indexPath];
    header.sectionTitle.text = _sectionArray[indexPath.section];
    header.sectionTitle.font = [UIFont systemFontOfSize:12];
    [header.sectionBtn addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    header.sectionBtn.tag = indexPath.section + 1;
    //    header.iconImage.backgroundColor = [UIColor purpleColor];
    
    if ([_stateArray[indexPath.section] isEqualToString:@"0"]) {
        //        header.iconImage.backgroundColor = [UIColor redColor];
        header.iconImage.image = [UIImage imageNamed:@"btn_menu_normal"];
        header.imgHeight.constant = 12.0f;
        header.imgWidth.constant = 7.0f;
        // header.sectionTitle.text = @"关闭";
        
    }
    else if ([_stateArray[indexPath.section] isEqualToString:@"1"]){
        header.iconImage.image = [UIImage imageNamed:@"btn_menu@2x.png"];
        header.imgHeight.constant = 7.0f;
        header.imgWidth.constant = 12.0f;
        // header.sectionTitle.text = @"打开";
        
    }
    return header;
}

- (void)buttonClick:(UIButton *)sender//headButton点击
{
    //判断状态值
    if ([_stateArray[sender.tag - 1] isEqualToString:@"1"]){
        //修改
        [_stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
    }else{
        [_stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
    }
    [_collectionView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1]];
    
    
}

@end
